package it.pir.es08ChatRMIPullDecorator;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DirtyMessage extends ChatDecorator implements Serializable {

	private static final long serialVersionUID = 1L;

	private Map<String,String> censoredMap;
	
	public DirtyMessage(Chat decoratedChat) throws RemoteException {
		super(decoratedChat);
		censoredMap = new HashMap<String,String>();
		initMap();
	}
	
	private void initMap(){
		censoredMap.put("(?i)idiota", "i****a");
		censoredMap.put("(?i)cretino", "c*****o");
		censoredMap.put("(?i)scemo", "s***o");
		censoredMap.put("(?i)ebete", "e***e");
		censoredMap.put("(?i)microsoft", "Linux");
		censoredMap.put("(?i)apple", "Linux");
	}
	
	public List<Message> receive(int lastId) throws RemoteException{
		List<Message> list = super.receive(lastId);
		censorMessage(list);
		return list;
	}
	
	private void censorMessage(List<Message> list){
		for(Message m: list){
			String text = m.getText();
			for(Map.Entry<String,String> e : censoredMap.entrySet()){
				text = text.replaceAll(e.getKey(), e.getValue());
			}
			m.setText(text);
			
		}		
	}
}
